package com.holdtime.jtt809.taxi.module.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.holdtime.jtt809.taxi.module.entity.PlatformLoginLogDO;
import com.holdtime.jtt809.taxi.module.entity.mapper.PlatformLoginLogMapper;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformLoginLogPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformLoginLogSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.PlatformLoginLogService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.holdtime.jtt809.taxi.module.enums.Taxi809ErrorCodeConstants.PLATFORM_LOGIN_LOG_NOT_EXISTS;

/**
 * 下级平台登陆日志 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class PlatformLoginLogServiceImpl implements PlatformLoginLogService {

    @Resource
    private PlatformLoginLogMapper platformLoginLogMapper;

    @Override
    public String createPlatformLoginLog(PlatformLoginLogSaveReqVO createReqVO) {
        // 插入
        PlatformLoginLogDO platformLoginLog = BeanUtils.toBean(createReqVO, PlatformLoginLogDO.class);
        platformLoginLogMapper.insert(platformLoginLog);
        // 返回
        return platformLoginLog.getId();
    }

    @Override
    public void updatePlatformLoginLog(PlatformLoginLogSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformLoginLogExists(updateReqVO.getId());
        // 更新
        PlatformLoginLogDO updateObj = BeanUtils.toBean(updateReqVO, PlatformLoginLogDO.class);
        platformLoginLogMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformLoginLog(String id) {
        // 校验存在
        validatePlatformLoginLogExists(id);
        // 删除
        platformLoginLogMapper.deleteById(id);
    }

    private void validatePlatformLoginLogExists(String id) {
        if (platformLoginLogMapper.selectById(id) == null) {
            throw exception(PLATFORM_LOGIN_LOG_NOT_EXISTS);
        }
    }

    @Override
    public PlatformLoginLogDO getPlatformLoginLog(String id) {
        return platformLoginLogMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformLoginLogDO> getPlatformLoginLogPage(PlatformLoginLogPageReqVO pageReqVO) {
        return platformLoginLogMapper.selectPage(pageReqVO);
    }

}